package com.hengpeng.itfinoss.controller.fund;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.util.MoneyUtil;
import com.hengpeng.itfinbase.constants.RespCodeConstants;
import com.hengpeng.itfinbase.constants.SystemConfigConstants;
import com.hengpeng.itfinbase.constants.TransCodeConstants;
import com.hengpeng.itfinbase.en.AccountType;
import com.hengpeng.itfinbase.en.TransType;
import com.hengpeng.itfinbase.message.ResponseMessage;
import com.hengpeng.itfinbase.message.partner.CashRequestMsg;
import com.hengpeng.itfinbase.message.partner.PartnerTransType;
import com.hengpeng.itfinbase.message.partner.PayRequestMsg;
import com.hengpeng.itfinbase.message.partner.QueryAcctsRequestMsg;
import com.hengpeng.itfinbase.message.partner.QueryAcctsResponseMsg;
import com.hengpeng.itfinbase.message.partner.TransferRequest;
import com.hengpeng.itfinbase.message.userInfo.AddMoneyRequest;
import com.hengpeng.itfinbase.persist.Account;
import com.hengpeng.itfinbase.persist.Partner;
import com.hengpeng.itfinbase.persist.Payment.PaymentType;
import com.hengpeng.itfinbase.persist.UserInfo;
import com.hengpeng.itfinoss.constant.SystemConfigUtil;
import com.hengpeng.itfinoss.controller.BaseController;
import com.hengpeng.itfinoss.service.ServiceFactory;

@Controller
@RequestMapping("/platform")
public class PlatfromController extends BaseController {
    @Autowired
    private ServiceFactory serviceFactory;

    @RequestMapping(value = "/load")
    public String list(ModelMap map, HttpServletRequest request) {
        Partner partner = serviceFactory.getPersistService().get(Partner.class,
                SystemConfigUtil.getConfigValue(SystemConfigConstants.PARTNER_ID));
        QueryAcctsRequestMsg requestMsg = new QueryAcctsRequestMsg();
        requestMsg.setPartnerTransType(PartnerTransType.QueryAccts);
        requestMsg.setMerCustId(partner.getMerchantId());
        requestMsg.setTransType(TransCodeConstants.PartnerTransCode.REQUEST);
        requestMsg.setBgRetUrl(service_notify_url);
        requestMsg.setRetUrl(page_return_url);
        QueryAcctsResponseMsg response = (QueryAcctsResponseMsg) serviceFactory.getRemoteService().remoteTrans(
                requestMsg);
        Long userId = Long.parseLong(SystemConfigUtil.getConfigValue(SystemConfigConstants.PARTNER_USERID));
        List<Account> platAccountList = serviceFactory.getAccountService().findByUserId(userId);
        map.put("queryAccts", response);
        map.put("partner", partner);
        map.put("paymentType", PaymentType.IMPREST);
        map.put("platAccList", platAccountList);
        return "fund/platform";
    }

    @RequestMapping(value = "/transferPlat2Person")
    @ResponseBody
    public ServiceResult<ResponseMessage> transferPlat2Person(ModelMap map, HttpServletRequest request,
            String outSubAccountId, String userName, String money) {
        ServiceResult<ResponseMessage> result = new ServiceResult<ResponseMessage>();
        UserInfo inUser = serviceFactory.getUserInfoService().getByUserName(userName);
        if (inUser == null) {
            result.setIsSuccess(false);
            result.setMessage("用户不存在");
            return result;
        }
        Account inAccount = serviceFactory.getAccountService().getByUserName(userName);

        UserInfo outUser = serviceFactory.getUserInfoService().getById(
                Long.parseLong(SystemConfigUtil.getConfigValue(SystemConfigConstants.PARTNER_USERID)));
        Account outAccount = serviceFactory.getAccountService().getByUserId(outUser.getId(), outSubAccountId);

        TransferRequest transferRequest = new TransferRequest();
        transferRequest.setOutType(AccountType.PLATFORM);// 固定出账户只能是平台账户
        transferRequest.setOutUserId(outUser.getId());
        transferRequest.setOutCustId(outAccount.getTrustAccountId());
        transferRequest.setOutAcctId(outAccount.getSubAccountId());
        transferRequest.setInType(AccountType.PERSONAL);
        transferRequest.setInUserId(inUser.getId());
        transferRequest.setInCustId(inAccount.getTrustAccountId());
        transferRequest.setInAcctId(inAccount.getSubAccountId());
        transferRequest.setTransAmt(MoneyUtil.convertFenToYuan(new BigDecimal(Double.parseDouble(money) * 100).longValue()));
        transferRequest.setPartnerTransType(PartnerTransType.Transfer);
        transferRequest.setTransType(TransCodeConstants.PartnerTransCode.REQUEST);
        transferRequest.setBgRetUrl(service_notify_url);
        transferRequest.setRetUrl(page_return_url);
        ResponseMessage res = (ResponseMessage) serviceFactory.getRemoteService().remoteTrans(transferRequest);
        if (!RespCodeConstants.CommonCode.TRANS_SUC.equals(res.getRespCode())) {
            result.setCode(RespCodeConstants.CommonCode.TRANS_FAIL);
            result.setMessage(res.getRespDesc());
        }
        return result;
    }

    @RequestMapping(value = "/addMoney")
    @ResponseBody
    public ServiceResult<ResponseMessage> addMoney(ModelMap map, HttpServletRequest request, String userName,
            String money, String remark) {
        ServiceResult<ResponseMessage> result = new ServiceResult<ResponseMessage>();
        UserInfo inUser = serviceFactory.getUserInfoService().getByUserName(userName);
        if (inUser == null) {
            result.setIsSuccess(false);
            result.setMessage("用户不存在");
            return result;
        }
        AddMoneyRequest transRequest = new AddMoneyRequest();
        transRequest.setUserId(inUser.getId());
        transRequest.setMoney(MoneyUtil.convertLongYuanToFen(Double.parseDouble(money)));
        transRequest.setReferenceId(inUser.getId().toString());
        transRequest.setTransType(TransCodeConstants.OtherTransCode.ADD_MONEY);
        transRequest.setType(TransType.ADDMONEY);
        transRequest.setRemark(remark);
        ResponseMessage res = (ResponseMessage) serviceFactory.getRemoteService().remoteTrans(transRequest);
        result.setReturnValue(res);
        return result;
    }

    @RequestMapping(value = "/transferPlat2Plat")
    @ResponseBody
    public ServiceResult<ResponseMessage> transferPlat2Plat(ModelMap map, HttpServletRequest request,
            String outSubAccountId, String inSubAccountId, String money) {
        UserInfo user = serviceFactory.getUserInfoService().getById(
                Long.parseLong(SystemConfigUtil.getConfigValue(SystemConfigConstants.PARTNER_USERID)));
        Account outAccount = serviceFactory.getAccountService().getByUserId(user.getId(), outSubAccountId);
        Account inAccount = serviceFactory.getAccountService().getByUserId(user.getId(), inSubAccountId);

        TransferRequest transferRequest = new TransferRequest();
        ServiceResult<ResponseMessage> result = new ServiceResult<ResponseMessage>();
        transferRequest.setOutType(AccountType.PLATFORM);// 固定出账户只能是平台账户
        transferRequest.setOutUserId(user.getId());
        transferRequest.setOutCustId(outAccount.getTrustAccountId());
        transferRequest.setOutAcctId(outAccount.getSubAccountId());
        transferRequest.setInType(AccountType.PLATFORM);
        transferRequest.setInUserId(user.getId());
        transferRequest.setInCustId(inAccount.getTrustAccountId());
        transferRequest.setInAcctId(inAccount.getSubAccountId());
        transferRequest.setTransAmt(MoneyUtil.convertFenToYuan(new BigDecimal(Double.parseDouble(money) * 100).longValue()));
        transferRequest.setPartnerTransType(PartnerTransType.Transfer);
        transferRequest.setTransType(TransCodeConstants.PartnerTransCode.REQUEST);
        transferRequest.setBgRetUrl(service_notify_url);
        transferRequest.setRetUrl(page_return_url);
        ResponseMessage res = (ResponseMessage) serviceFactory.getRemoteService().remoteTrans(transferRequest);
        result.setReturnValue(res);
        return result;
    }

    @RequestMapping(value = "/payment")
    @ResponseBody
    public ServiceResult<ResponseMessage> payment(ModelMap map, HttpServletRequest request, PaymentType type,
            String money) {
        ServiceResult<ResponseMessage> result = new ServiceResult<ResponseMessage>();
        if (type == PaymentType.IMPREST) {// 充值
            PayRequestMsg reqMsg = new PayRequestMsg();
            reqMsg.setUserId(Long.parseLong(SystemConfigUtil.getConfigValue(SystemConfigConstants.PARTNER_USERID)));
            reqMsg.setTransAmt(money);
            reqMsg.setPartnerTransType(PartnerTransType.NetSave);
            reqMsg.setTransType(TransCodeConstants.PartnerTransCode.REQUEST);
            reqMsg.setBgRetUrl(service_notify_url);
            reqMsg.setRetUrl(page_return_url);
            ResponseMessage response = (ResponseMessage) serviceFactory.getRemoteService().remoteTrans(reqMsg);
            result.setReturnValue(response);
        } else if (type == PaymentType.DRAW) {
            CashRequestMsg reqMsg = new CashRequestMsg();
            reqMsg.setUserId(Long.parseLong(SystemConfigUtil.getConfigValue(SystemConfigConstants.PARTNER_USERID)));
            reqMsg.setTransAmt(money);
            reqMsg.setPartnerTransType(PartnerTransType.Cash);
            reqMsg.setTransType(TransCodeConstants.PartnerTransCode.REQUEST);
            reqMsg.setBgRetUrl(service_notify_url);
            reqMsg.setRetUrl(page_return_url);
            ResponseMessage response = (ResponseMessage) serviceFactory.getRemoteService().remoteTrans(reqMsg);
            result.setReturnValue(response);
        }
        return result;
    }
}
